using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace ShareTools.ListsManager
{
    public partial class ListsManager : Form
    {
        public ListsManager()
        {
            InitializeComponent();
        }

        private void btnGetLists_Click(object sender, EventArgs e)
        {
            using (SPSite spSite = new SPSite(txtSiteURL.Text))
            {
                using (SPWeb spWeb = spSite.RootWeb)
                {
                    //Code to execute
                    cmbBoxSPList.Items.Clear();
                    foreach (SPList listItem in spWeb.Lists)
	                {
                        string tmp = listItem.Title;
                        cmbBoxSPList.Items.Add(listItem.Title);
                        cmbBoxSPList.SelectedIndex = 0;
	                }                     
                }
            }
        }

        private static StringBuilder BuildBatchDeleteCommand(SPList spList)
        {
            StringBuilder sbDelete = new StringBuilder();
            sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
            string command = "<Method><SetList Scope=\"Request\">" + spList.ID +
                "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{1}</SetVar></Method>";
            int i = 1;
            foreach (SPListItem item in spList.Items)
            {
                sbDelete.Append(string.Format(command, item.ID.ToString(), "/advisor/" + spList.Title + "/" + item.Name));
                i++;
            }
            sbDelete.Append("</Batch>");
            return sbDelete;
        }

        private void btnDeleteListItems_Click(object sender, EventArgs e)
        {
            StringBuilder sbDelete = new StringBuilder();
            using (SPSite spSite = new SPSite(txtSiteURL.Text))
            {
                using (SPWeb spWeb = spSite.RootWeb)
                {
                    SPList spList = spWeb.Lists[cmbBoxSPList.SelectedItem.ToString()];
                    sbDelete = BuildBatchDeleteCommand(spList);
                    richTxtResultMessage.Text = "Ignore this text:\n"+ sbDelete.ToString();
                    //DialogResult dr = MessageBox.Show("conti..?", "continue..", MessageBoxButtons.YesNo);
                    //if (dr == DialogResult.Yes)
                    //{
                        try
                        {
                            string result = spWeb.ProcessBatchData(sbDelete.ToString());
                            richTxtResultMessage.Text = "Successfully deleted the items in the selected list.";
                            richTxtResultMessage.Text += "\n\nBatch Process Result:\n" + result;
                        }
                        catch (Exception ex)
                        {
                            //Console.WriteLine("Delete failed: " + ex.Message);
                            richTxtResultMessage.Text = ex.ToString();
                            MessageBox.Show(ex.ToString());
                            //throw;
                        }
                    //}
                }
            }
 
        }

        private void btnDeleteList_Click(object sender, EventArgs e)
        {
            using (SPSite spSite = new SPSite(txtSiteURL.Text))
            {
                using (SPWeb spWeb = spSite.RootWeb)
                {
                    try
                    {
                        SPList spList = spWeb.Lists[cmbBoxSPList.SelectedItem.ToString()];
                        spList.Delete();
                        richTxtResultMessage.Text = "Successfully deleted the list";
                    }
                    catch (Exception ex)
                    {
                        richTxtResultMessage.Text = ex.ToString();
                    }
                }
            }
        }
    }
}